package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private CategoryFeignClient categoryFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;



    @Override
    public Map<String, Object> getItem(Long albumId) {
        //  声明一个map 集合
        Map<String,Object> map = new HashMap<>();
        //  多线程：
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //  远程调用
            Result<AlbumInfo> infoResult = albumInfoFeignClient.getAlbumInfo(albumId);
            Assert.notNull(infoResult, "专辑结果集不存在");
            AlbumInfo albumInfo = infoResult.getData();
            Assert.notNull(albumInfo, "专辑信息不存在");
            map.put("albumInfo",albumInfo);

            //  返回数据
            return albumInfo;
        },threadPoolExecutor);

        //  获取统计状态信息
        CompletableFuture<Void> statCompletableFuture = CompletableFuture.runAsync(() -> {
            //  远程调用
            Result<AlbumStatVo> statVoResult = albumInfoFeignClient.getAlbumStatVo(albumId);
            Assert.notNull(statVoResult, "专辑统计信息不存在");
            AlbumStatVo albumStatVo = statVoResult.getData();
            Assert.notNull(albumStatVo, "专辑统计信息不存在");
            //  存储数据.
            map.put("albumStatVo", albumStatVo);
        },threadPoolExecutor);

        CompletableFuture<Void> categoryCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            //  获取分类数据
            Result<BaseCategoryView> categoryViewResult = categoryFeignClient.getCategoryView(albumInfo.getCategory3Id());
            Assert.notNull(categoryViewResult, "分类数据不存在");
            BaseCategoryView baseCategoryView = categoryViewResult.getData();
            Assert.notNull(baseCategoryView, "分类数据不存在");
            map.put("baseCategoryView", baseCategoryView);
        },threadPoolExecutor);

        //  获取用户数据
        CompletableFuture<Void> userCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            Result<UserInfoVo> infoVoResult = userInfoFeignClient.getUserInfoVo(albumInfo.getUserId());
            Assert.notNull(infoVoResult, "用户信息不存在");
            UserInfoVo userInfoVo = infoVoResult.getData();
            Assert.notNull(userInfoVo, "用户信息不存在");
            map.put("announcer", userInfoVo);
        },threadPoolExecutor);

        //  多任务组合
        CompletableFuture.allOf(albumInfoCompletableFuture, statCompletableFuture, categoryCompletableFuture, userCompletableFuture).join();
        //  返回数据
        return map;
    }

}
